package superbird;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * 面板类 ，继承 JPanel
 * @author 廖佳
 *
 */
public class BirdGameJpanel extends JPanel{

	/**
	 *
	 */
	private static final long serialVersionUID = -4295440785601791952L;
	//面板的属性对应游戏中应有的状态
	public int state = 0;
	//游戏分数
	public int score = 0;
	//游戏的三种状态值
	public static final int START = 0;   //开始状态
	public static final int RUNNING = 1; //运行状态
	public static final int GAMEOVER = 2;//结束状态

	//用Image的实现类BufferImage来保存图片
	public BufferedImage startImage = null;
	public BufferedImage backGround = null;
	public BufferedImage overImage = null;

	//声明小鸟，管道，地面类
	Bird bird;
	Column column1;
	Column column2;
	Ground ground;

	//构造方法,初始化面板的属性
	public BirdGameJpanel() throws IOException{
		score = 0; //分数为0
		state = START; //游戏状态为开始状态

		//将图片加载到内存
		startImage = ImageIO.read(getClass().getResource("start.png"));
		backGround = ImageIO.read(getClass().getResource("bg.png"));
		overImage = ImageIO.read(getClass().getResource("gameover.png"));

		//实例化小鸟，管道，地面类
		bird = new Bird();
		column1 = new Column(1);
		column2 = new Column(2);
		ground = new Ground();

	}

	//重写Jpanel中的绘画的功能
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		//参数列表：图片Image对象，x ，y 坐标值 ，null
		g.drawImage(backGround, 0, 0, null);
		//参数列表：字体 字体类型 字号
		Font font  = new Font(Font.SANS_SERIF, Font.BOLD, 40);
		g.setFont(font);//把字体设置到绘画方法
		g.setColor(Color.BLACK);

		//创建旋转2D画笔，绘制动态的小鸟
		Graphics2D graphics2d = (Graphics2D) g;
		//旋转画布参数列表 旋转的弧度，旋转的中心
		graphics2d.rotate(-bird.rad, bird.x, bird.y);

		//绘制小鸟
		g.drawImage(bird.image, bird.x - bird.width/2, bird.y - bird.height, null);//bird.y 应该要减height/2

		//转正画布
		graphics2d.rotate(bird.rad,bird.x, bird.y);

		//绘制管道
		g.drawImage(column1.image, column1.x - column1.width/2, column1.y-column1.height/2, null);
		g.drawImage(column2.image, column2.x - column2.width/2, column2.y-column2.height/2, null);

		//绘制分数
		g.drawString(score+"", 40, 60);

		//绘制地面
		g.drawImage(ground.image, ground.x, ground.y, null);

		//根据游戏的状态绘制开始界面或者结束界面
		if (state == START) {
			g.drawImage(startImage, 0, 0, null);
		}
		else if(state == GAMEOVER){
			g.drawImage(overImage, 0, 0, null);
		}

	}

	public void action() throws InterruptedException {
		MouseListener listener = new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);

				switch (state) {
					//start
				case START:
					state = RUNNING;//游戏开始之后状态就变成运行状态
					break;

					//Game Run
				case RUNNING:

					bird.flyUp();
					break;

					//Game over
				case GAMEOVER:
					state = START;
					score = 0;

					try {
						bird = new Bird();
						column1 = new Column(1);
						column2 = new Column(2);
						ground = new Ground();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					break;
				}

			}

		};
		this.addMouseListener(listener);

		//死循环
		while(true){
			switch (state) {
			case START:
//				bird.fly();
				break;

			case RUNNING:

				ground.step();
				bird.fly();
				column1.step();
				column2.step();
				bird.step();

				//得分
				if(bird.x == column1.x || bird.x == column2.x)
					score++;

				//
				if(bird.hit(column1) || bird.hit(column2) || bird.hit(ground)){
					state = GAMEOVER;
				}


				break;
			case GAMEOVER:

				break;
			}
			//重复绘制
			repaint();
			Thread.sleep(50/3);
		}
	}

}
